var express = require('express');
var sign = require('./controllers/sign');
var admin = require('./controllers/admin');
var event = require('./controllers/event');
var user = require('./controllers/user');
var area = require('./controllers/area');
var position = require('./controllers/position');
var group = require('./controllers/group');
var staff = require('./controllers/staff');
var dashboard = require('./controllers/dashboard');
var checkin = require('./controllers/checkin');
var auth = require('./middlewares/auth');
var config = require('config-lite');
var crypto = require('crypto');
var path = require('path');

var router = new express.Router();

// 上传相关
var file = require('./controllers/file');
var multer  = require('multer');
var storage = multer.diskStorage({ //https://github.com/expressjs/multer/issues/170#issuecomment-123402678
  destination: config.uploadDir,
  filename: function (req, file, cb) {
    crypto.pseudoRandomBytes(16, function (err, raw) {
      if (err) {
        return cb(err);
      }
      cb(null, raw.toString('hex') + path.extname(file.originalname.toLocaleLowerCase()));
    });
  }
});
var upload = multer({ storage: storage });

router.post('/file/upload', [upload.single('file')], file.upload);

router.post('/signin/get_accesstoken', sign.getAccessToken);

router.get('/gen_uuid', sign.genUUID);

router.get('/', dashboard.indexPage);

router.post('/checkin', [auth.userRequired], checkin.checkin);
router.get('/stats/infos', [auth.userRequired], dashboard.infos);
router.get('/stats/chart', [auth.userRequired], dashboard.chart);

//TODO: 增加权限验证中间件，不同权限的人有些接口不可以访问
router.post('/events', [auth.userRequired], event.newEvent);
router.post('/events/:uuid', [auth.userRequired], event.newEvent);
router.post('/events/:uuid/del', [auth.userRequired], event.delEvent);
router.post('/events/:uuid/enable', [auth.userRequired], event.enableEvent);
router.post('/events/:uuid/disable', [auth.userRequired], event.disableEvent);
router.get('/events', [auth.userRequired], event.getEvents);
router.get('/events/:uuid', [auth.userRequired], event.getEventByUUID);

router.post('/staffs', [auth.userRequired], staff.newStaff);
router.get('/staffs', [auth.userRequired], staff.getStaffs);
router.get('/staffs/:uuid', [auth.userRequired], staff.getStaffByUUID);
router.get('/staffs/verify/:uuid', [auth.userRequired], staff.vefiryStaff);
router.get('/staffs/:uuid/qrcode', staff.qrcodeLink);
router.post('/staffs/batch_update', [auth.userRequired], staff.batchUpdateStaff);
router.post('/staffs/:uuid', [auth.userRequired], staff.newStaff);
router.post('/staffs/:uuid/export', [auth.userRequired], staff.exportStaff);
router.post('/staffs/:uuid/del', [auth.userRequired], staff.delStaff);

router.post('/users', [auth.userRequired], user.newUser);
router.post('/users/:uuid', [auth.userRequired], user.newUser);
router.post('/users/:uuid/del', [auth.userRequired], user.delUser);
router.post('/users/:uuid/enable', [auth.userRequired], user.enableUser);
router.post('/users/:uuid/disable', [auth.userRequired], user.disableUser);
router.get('/users', [auth.userRequired], user.getUsers);
router.get('/users/:uuid', [auth.userRequired], user.getUserByUUID);
router.get('/user/gen_pcode', [auth.userRequired], user.genPcode);

router.post('/areas', [auth.userRequired], area.newArea);
router.post('/areas/:uuid', [auth.userRequired], area.newArea);
router.get('/areas', [auth.userRequired], area.getAreas);
router.get('/areas/:uuid', [auth.userRequired], area.getAreaByUUID);
router.post('/areas/:uuid/del', [auth.userRequired], area.delArea);

router.post('/positions', [auth.userRequired], position.newPosition);
router.post('/positions/:uuid', [auth.userRequired], position.newPosition);
router.get('/positions', [auth.userRequired], position.getPositions);
router.get('/positions/:uuid', [auth.userRequired], position.getPositionByUUID);
router.post('/positions/:uuid/del', [auth.userRequired], position.delPosition);

router.post('/groups', [auth.userRequired], group.newGroup);
router.post('/groups/:uuid', [auth.userRequired], group.newGroup);
router.get('/groups', [auth.userRequired], group.getGroups);
router.get('/groups/:uuid', [auth.userRequired], group.getGroupByUUID);
router.post('/groups/:uuid/del', [auth.userRequired], group.delGroup);

module.exports = router;
